package com.open.capacity.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Notice;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.dao.NoticeDao;
import com.open.capacity.user.dao.NoticeRoleDao;
import com.open.capacity.user.service.NoticeService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired(required = false)
    NoticeDao noticeDao;
    @Autowired(required = false)
    NoticeRoleDao noticeRoleDao;

    @Override
    public PageResult getSelfPublishNotice(Map map) {
        if (MapUtils.getInteger(map, "page") != null && MapUtils.getInteger(map, "limit") != null) {
            PageHelper.startPage(MapUtils.getInteger(map, "page"), MapUtils.getInteger(map, "limit"), true);
        }
        List<Notice> list = noticeDao.getSelfPublishNotice(map);
        List<Notice> collect = list.stream().map(notice -> {
            notice.setRoles(noticeRoleDao.findRolesByNoticeId(notice.getNticid()));
            return notice;
        }).collect(Collectors.toList());

        PageInfo<Notice> pageInfo = new PageInfo<>(collect);
        return PageResult.<Notice>builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    @Override
    public Result getNoReadNoticeCount(Long userid) {
        try {
            return Result.succeed(noticeDao.getNoReadNoticeCount(userid), "未读通知数");
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @Override
    public PageResult getReceivedAndMarkNoReadNotice(Map map) {
        try {
            if (MapUtils.getInteger(map, "page") != null && MapUtils.getInteger(map, "limit") != null) {
                PageHelper.startPage(MapUtils.getInteger(map, "page"), MapUtils.getInteger(map, "limit"), true);
            }
            List<Notice> list = noticeDao.getReceivedAndMarkNoReadNotice(map);
            List<Notice> collect = list.stream().map(notice -> {
                notice.setRoles(noticeRoleDao.findRolesByNoticeId(notice.getNticid()));
                return notice;
            }).collect(Collectors.toList());
            PageInfo<Notice> pageInfo = new PageInfo<>(collect);
            return PageResult.<Notice>builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @Override
    public Result readNotice(Integer nticid, Long userid) {
        try {
            if (noticeDao.UserIsReadNotice(nticid,userid)>0)
                return Result.succeed("已阅");
            return noticeDao.readNotice(nticid, userid) > 0 ? Result.succeed("已阅") : Result.failed("查阅失败");
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @Override
    public Result publishOrUpdateNotice(Notice notice) {
        try {
            int res = 0;
            if (notice.getNticid() == null)
                res = noticeDao.publishNotice(notice);
            else {
                res = noticeDao.updateNotice(notice);
            }
            noticeRoleDao.deleteNoticeRole(notice.getNticid(), null);
            List roleIds = Arrays.asList(notice.getRoleId().split(","));
//            if (!CollectionUtils.isEmpty(roleIds)) {
//                roleIds.forEach(roleId -> {
//                    noticeRoleDao.saveNoticeRoles(notice.getNticid(), Long.parseLong(roleId.toString()));
//                });
//            }
            noticeRoleDao.saveNoticeRoles(notice.getNticid(), roleIds);
            return  res> 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @Override
    public Result deleteNotice(Integer nticid) {
        try {
            return noticeDao.deleteNotice(nticid) > 0 ? Result.succeed("删除成功") : Result.failed("删除失败");
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }
}
